package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * SetmealController
 * Created by jiachun.lu on 2024/06/25.
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPageInfo = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, queryWrapper);

        BeanUtils.copyProperties(pageInfo, dtoPageInfo, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            String categoryName = null;
            Category category = categoryService.getById(record.getCategoryId());
            if (category != null) {
                categoryName = category.getName();
            }
            setmealDto.setCategoryName(categoryName);
            list.add(setmealDto);
        }
        dtoPageInfo.setRecords(list);
        return R.success(dtoPageInfo);
    }

    @GetMapping("/{id}")
    public R<Setmeal> getById(@PathVariable Long id) {
        SetmealDto setmeal = setmealService.getByIdWithDish(id);
        if (setmeal != null) {
            return R.success(setmeal);
        }
        return R.error("套餐不存在");
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        try {
            setmealService.updateWithDish(setmealDto);
            return R.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("修改失败");
        }
    }

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        try {
            setmealService.saveWithDish(setmealDto);
            return R.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("新增失败");
        }
    }

    @DeleteMapping
    public R<String> delete(@RequestParam Long[] ids) {
        try {
            setmealService.deleteWithDish(ids);
            return R.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("删除失败");
        }
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam Long[] ids) {
        log.info("修改套餐状态：{}", status);
        try {
            setmealService.updateStatus(status, ids);
            return R.success("修改套餐状态成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("修改套餐状态失败");
        }
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealService.list(queryWrapper);
        return R.success(setmeals);
    }
}
